import { fetchList } from "./index";
import {
  drawerFormState,
  operateType,
  orderStatusMap,
  searchOrderStateOptions,
  searchPartyOptions,
  searchPaymentMethodOptions,
  wrapRef,
} from "./store";
export const handleSubmit = async ({ loading, visible }: any) => {
  try {
    loading.value = true;
    if (operateType.value === "add") {
      const res: any = await rechargeApi.createRechargeRule({
        name: drawerFormState.name,
        is_wallet_amount_limit: drawerFormState.is_wallet_amount_limit,
        wallet_amount_limit: drawerFormState.wallet_amount_limit ?? "",
        amount_list: drawerFormState.amount_list?.join(","),
        is_recharge_amount_limit: drawerFormState.is_recharge_amount_limit,
        min_recharge_amount: drawerFormState?.min_recharge_amount ?? "",
        max_recharge_amount: drawerFormState?.max_recharge_amount ?? "",
        comment: drawerFormState.comment,
      });
      if (res.code != 200) return message.error(res.message);
      message.success($t("base.addSuccess"));
      fetchList();
    }

    if (operateType.value === "update") {
      const res: any = await rechargeApi.updateRechargeRule({
        id: drawerFormState.id,
        name: drawerFormState.name,
        is_wallet_amount_limit: drawerFormState.is_wallet_amount_limit ?? "",
        wallet_amount_limit: !isEmptyType(drawerFormState.wallet_amount_limit)
          ? drawerFormState.wallet_amount_limit
          : "",
        amount_list: drawerFormState.amount_list?.join(","),
        is_recharge_amount_limit:
          drawerFormState.is_recharge_amount_limit ?? "",
        min_recharge_amount: drawerFormState?.min_recharge_amount ?? "",
        max_recharge_amount: drawerFormState?.max_recharge_amount ?? "",
        comment: drawerFormState.comment ?? "",
      });
      if (res.code != 200) return message.error(res.message);
      message.success($t("base.updateSucess"));
      fetchList();
    }

    if (operateType.value === "view") {
    }
  } catch (error: any) {
    message.error(error.message);
  } finally {
    loading.value = false;
    visible.value = false;
  }
};

export const calcWrapHeight = () => {
  nextTick(() => {
    const distance = getTopDistance(wrapRef.value);
    wrapRef.value.style.height = `calc(100vh - ${distance}px - 10px - 62px)`;
  });
};
export const fetchSearchMap = async () => {
  try {
    const res: any = await rechargeApi.quertySearchDicts();
    if (res.code != 200) return message.error(res.message);
    searchPartyOptions.value = objectToOptions(res.data.platformList, "number");
    searchPaymentMethodOptions.value = [
      { label: $t("dict6"), value: "Infinity" },
      ...(Object.values(res.data?.passagewayList)?.map((item: any) => ({
        label: item,
        value: item,
      })) ?? []),
    ];
    initObjAttr(orderStatusMap, [
      { label: $t("dict6"), value: Infinity },
      ...objectToOptions(res.data.statusList, "number"),
    ]);
    searchOrderStateOptions.value = deepClone(orderStatusMap);
  } catch (error: any) {
    message.error(error.message);
  }
};
